package com.atgouwu.mall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atgouwu.mall.common.utils.R;
import com.atgouwu.mall.ware.feign.MemberFeignService;
import com.atgouwu.mall.ware.vo.FareVo;
import com.atgouwu.mall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atgouwu.mall.common.utils.PageUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.atgouwu.mall.ware.dao.WareInfoDao;
import com.atgouwu.mall.ware.entity.WareInfoEntity;
import com.atgouwu.mall.ware.service.WareInfoService;

@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    private MemberFeignService memberFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
        new Page<>(Long.valueOf(String.valueOf(params.get("page"))),Long.valueOf(String.valueOf(params.get("size")))),
                new QueryWrapper<WareInfoEntity>()
        );
        return new PageUtils(page.getRecords(), (int) page.getTotal(), (int) page.getSize(), (int) page.getCurrent());
    }

    //获取运费
    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R addrInfo = memberFeignService.info(addrId); //获取收获地址详细信息(远程调用member会员服务)
        MemberAddressVo memberAddressVo = addrInfo.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {});
        if (memberAddressVo != null) {
            String phone = memberAddressVo.getPhone();
            String fare = phone.substring(phone.length() - 10, phone.length()-8); //运费(截取手机号码最后一位)
            BigDecimal bigDecimal = new BigDecimal(fare);
            fareVo.setFare(bigDecimal);
            fareVo.setAddress(memberAddressVo);
            return fareVo;
        }
        return null;
    }

}